package com.gdqy.FCS.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.stat.TableStat.Mode;
import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.Fileupload;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Brand;
import com.gdqy.FCS.entity.Clothing;
import com.gdqy.FCS.entity.ClothingType;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.Dictionary;
import com.gdqy.FCS.entity.EnterpriseInfo;
import com.gdqy.FCS.entity.EnterpriseRange;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.service.BrandService;
import com.gdqy.FCS.service.ClothingService;
import com.gdqy.FCS.service.ClothingTypeService;
import com.gdqy.FCS.service.DictionaryService;
import com.gdqy.FCS.service.EnterpriseInfoService;
import com.gdqy.FCS.service.EnterpriseRangeService;
import com.gdqy.FCS.service.InformationService;
import com.gdqy.FCS.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("enterprise")
public class EnterpriseInfoController {

	@Autowired
	private EnterpriseInfoService enterpriseInfoService;
	@Autowired
	private EnterpriseRangeService enterpriseRangeService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private ClothingService clothingService;
	@Autowired
	private ClothingTypeService clothingTypeService;
	@Autowired
	private UserService userService;
	@Autowired
	private InformationService informationService;
	
	//企业个人中心
	@RequestMapping("enterprise")
	public ModelAndView enterprise(@RequestParam(value="user_id",required=true) int user_id){
		ModelAndView modelAndView = new ModelAndView();
		//获取企业信息
		EnterpriseInfo enterpriseInfo = enterpriseInfoService.selectByUser_id(user_id);
	
		if(enterpriseInfo != null){
			int enterprise_id = enterpriseInfo.getId();
			modelAndView.addObject("enterpriseInfo", enterpriseInfo);
			//企业经营范围
			List<EnterpriseRange> enterpriseRanges =  enterpriseRangeService.selectByEnterprise_id(enterprise_id);
			modelAndView.addObject("enterpriseRanges", enterpriseRanges);
			//品牌数量
			int brand_count = brandService.selectCountByEnterprise_id(enterprise_id);
			modelAndView.addObject("brand_count", brand_count);
			//服装数量
			int clothing_count = clothingService.selectCountByEnterprise_id(enterprise_id);
			modelAndView.addObject("clothing_count", clothing_count);
			modelAndView.setViewName("enterprise/enterprise");
			//资讯数量
			int information_count = informationService.selectCountByRelease_id(user_id);
			modelAndView.addObject("information_count", information_count);
		}else{
			modelAndView.addObject("msg", "不存在该企业");
			modelAndView.setViewName("redirect:common/error");
		}
		return modelAndView;
	}
	
	
	/************************************************账号管理************************************************************/
	//修改企业信息--获取数据
	@RequestMapping("deal_changeInfo")
	public ModelAndView deal_changeInfo(int enterprise_id){
		ModelAndView modelAndView = new ModelAndView();
		//企业信息
		EnterpriseInfo enterpriseInfo = enterpriseInfoService.selectById(enterprise_id);
		modelAndView.addObject("enterpriseInfo", enterpriseInfo);
		//字典中的企业经营范围
		List<ClothingType> dic_enterpriseRanges = clothingTypeService.selectClothing_class("");
		modelAndView.addObject("dic_enterpriseRanges", dic_enterpriseRanges);
		//企业经营范围
		List<EnterpriseRange> enterpriseRanges =  enterpriseRangeService.selectByEnterprise_id(enterprise_id);
		modelAndView.addObject("enterpriseRanges", enterpriseRanges);
		modelAndView.setViewName("enterprise/enter_changeInfor");
		return modelAndView;
	}
	
	//真正修改操作
	@RequestMapping("changeInfo")
	public ModelAndView changeInfo(EnterpriseInfo enterpriseInfo,MultipartFile logo_pic) throws IllegalStateException, IOException{
		System.out.println("this is a test......");
		User user = userService.selectById(enterpriseInfo.getUser_id());
		String originalFilename =  logo_pic.getOriginalFilename();
		int enterprise_id = enterpriseInfo.getId();
		//头像处理
		if(logo_pic != null && originalFilename!=null && originalFilename!="" ){
		Fileupload fileupload = new Fileupload();
		String url=fileupload.upload(logo_pic, "enterprise", user.getUsername(), "enterprise-info");
		enterpriseInfo.setLogo_link(url);
		
		user.setPhoto_link(url);
		userService.update(user);
		}
		
		enterpriseInfo.setUpdate_time(new Date());
		enterpriseInfoService.update(enterpriseInfo);
		return new ModelAndView("redirect:/enterprise/deal_changeInfo.do?enterprise_id="+enterprise_id);
	}
	
	//浏览企业
	@RequestMapping("scan_enterprise")
	public ModelAndView scan_enterprise(){
		ModelAndView modelAndView = new ModelAndView();
		//女装企业
		List<EnterpriseInfo> enterpriseInfo_lady = enterpriseInfoService.selectByRangeType_code("CL002");
		modelAndView.addObject("enterpriseInfo_lady", enterpriseInfo_lady);
		//男装企业
		List<EnterpriseInfo> enterpriseInfo_men = enterpriseInfoService.selectByRangeType_code("CL001");
		modelAndView.addObject("enterpriseInfo_men", enterpriseInfo_men);
		//童装企业
		List<EnterpriseInfo> enterpriseInfo_child = enterpriseInfoService.selectByRangeType_code("CL003");
		modelAndView.addObject("enterpriseInfo_child", enterpriseInfo_child);
		//内衣企业
		List<EnterpriseInfo> enterpriseInfo_underwear = enterpriseInfoService.selectByRangeType_code("CL006");
		modelAndView.addObject("enterpriseInfo_underwear", enterpriseInfo_underwear);
		//婚纱企业
		List<EnterpriseInfo> enterpriseInfo_wedding = enterpriseInfoService.selectByRangeType_code("CL004");
		modelAndView.addObject("enterpriseInfo_wedding", enterpriseInfo_wedding);
		//毛衣企业
		List<EnterpriseInfo> enterpriseInfo_sweater= enterpriseInfoService.selectByRangeType_code("CL005");
		modelAndView.addObject("enterpriseInfo_sweater", enterpriseInfo_sweater);
		//企业数量
		int enterprise_count = enterpriseInfoService.selectAllCount();
		modelAndView.addObject("enterprise_count", enterprise_count);
		
		modelAndView.setViewName("enterprise/allList");
		return modelAndView;
	}
	
	//企业简介
	@RequestMapping("enterprise_introduction")
	public ModelAndView enterprise_introduction(int enterprise_id) {
		ModelAndView modelAndView = new ModelAndView();
		//品牌数
		int brand_count = brandService.selectCountByEnterprise_id(enterprise_id);
		modelAndView.addObject("brand_count", brand_count);
		//服装数
		int clothing_count = clothingService.selectCountByEnterprise_id(enterprise_id);
		modelAndView.addObject("clothing_count", clothing_count);
		//品牌详情
		List<Brand> brands = brandService.selectByEnterprise_id(enterprise_id);
		modelAndView.addObject("brands", brands);
		//企业信息
		EnterpriseInfo enterpriseInfo = enterpriseInfoService.selectById(enterprise_id);
		modelAndView.addObject("enterpriseInfo", enterpriseInfo);
		//经营范围
		List<EnterpriseRange> enterpriseRange = enterpriseRangeService.selectByEnterprise_id(enterprise_id);
		modelAndView.addObject("enterpriseRange", enterpriseRange);
		//人气
		int enterprise_popularity = clothingService.selectPopularityByEnterprise_id(enterprise_id);
		modelAndView.addObject("enterprise_popularity", enterprise_popularity);
		modelAndView.setViewName("enterprsie/enterprise_introduction");
		return modelAndView;
	}
	
	//通过经营范围返回对应的企业
	@RequestMapping("listByRangeCode")
	public ModelAndView listByRangeCode(String rangeType_code,Args args,int pageindex, int pagesize) {
		ModelAndView modelAndView = new ModelAndView();
		List<ClothingType> clothingTypes = clothingTypeService.selectClothing_class("");
		modelAndView.addObject("clothingTypes", clothingTypes);
		ClothingType clothingType = clothingTypeService.selectByCode(rangeType_code);
		modelAndView.addObject("clothingType", clothingType);
		//分页开始
				PageHelper.startPage(pageindex, pagesize);
		List<EnterpriseInfo> enterpriseInfos = enterpriseInfoService.selectByRangeType_code(rangeType_code);
				PageInfo<EnterpriseInfo> page = new PageInfo<EnterpriseInfo>(enterpriseInfos);
				ListResponse<EnterpriseInfo> json= FastResponse._().errCode(0).subCode(0).errMsg("获取企业信息成功")
						.args(args.getReqeustargs()).pageIndex(page.getPageNum())
						.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
						.hasNext(page.isHasNextPage()).list(enterpriseInfos);
				//分页结束
		modelAndView.addObject("json", json);
		modelAndView.setViewName("enterprise/enterprise_list");
		return modelAndView;
	}
}